<?php

declare(strict_types=1);
// +----------------------------------------------------------------------
// | swiftAdmin 极速开发框架 [基于WebMan开发]
// +----------------------------------------------------------------------
// | Copyright (c) 2020-2030 http://www.swiftadmin.net
// +----------------------------------------------------------------------
// | swiftAdmin.net High Speed Development Framework
// +----------------------------------------------------------------------
// | Author: meystack <coolsec@foxmail.com> Apache 2.0 License
// +----------------------------------------------------------------------
namespace app\admin\controller\system;


use app\AdminController;
use app\common\library\Ip2Region;
use app\common\model\system\Kehu as Kehus;
use support\Response;
use system\Random;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use Webman\Http\Request;

/**
 * 用户管理
 * Class User
 * @package app\admin\controller\system
 */
class Kehu extends AdminController
{
    // 初始化函数
    public function __construct()
    {
        parent::__construct();
        $this->model = new Kehus();
    }

    /**
     * 获取资源
     * @return Response
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function index(): \support\Response
    {

        if (request()->isAjax()) {
            // 获取数据
            $post = \request()->all();
            $page = (int)input('page') ?? 1;
            $limit = (int)input('limit') ?? 10;
            // 生成查询条件
            $where = array();
            if (!empty($post['title'])) {
                $where[] = ['name|mobile', 'like', '%' . $post['title'] . '%'];
            }
            $count = $this->model->where($where)->count();
            $page = ($count <= $limit) ? 1 : $page;
            $list = $this->model->where($where)->order("id desc")->limit((int)$limit)->page((int)$page)->select();
            foreach ($list as $row) {
                $row['latlon'] = $row['lon'] . ',' . $row['lat'];
            }
            // TODO..
            return $this->success('查询成功', "", $list, $count);
        }
        return view('/system/kehu/index', []);
    }
 

    /**
     * 编辑会员
     */
    public function edit()
    {

        if (request()->isPost()) {
            $post = \request()->post();
            if (empty($post['latlon'])) {
                return $this->error('坐标参数错误,请重试');
            }
            $latlon =  explode(',', $post['latlon']);
            if (count($latlon) != 2) {
                return $this->error('坐标参数错误,请重试');
            }
            $post['lat'] = $latlon[1];
            $post['lon'] = $latlon[0];
            if ($this->model->update($post)) {
                return $this->success('更新成功！');
            }
            return $this->error('更新失败！');
        }
    }

        /**
     * 编辑会员
     */
    public function add()
    {

        if (request()->isPost()) {
            $post = \request()->post();
            if (empty($post['latlon'])) {
                return $this->error('坐标参数错误,请重试');
            }
            $latlon =  explode(',', $post['latlon']);
            if (count($latlon) != 2) {
                return $this->error('坐标参数错误,请重试');
            }
            $post['lat'] = $latlon[1];
            $post['lon'] = $latlon[0];
            if ($this->model->create($post)) {
                return $this->success('更新成功！');
            }
            return $this->error('更新失败！');
        }
    }
    /**
     * 删除轮播图
     */
    public function del()
    {
        $id = input('id');
        if (!empty($id) && is_numeric($id)) {
            if ($this->model::destroy($id)) {
                return $this->success('删除成功！');
            }
        }
        return $this->error('删除失败，请检查您的参数！');
    }

}
